<template>
  <view class="my-box">
    <view class="head">
      <image v-if="userInfoStore.avatarUrl" class="image" :src="userInfoStore.avatarUrl"></image>
      <image class="image" v-else src="../../../assets/gaode.png"></image>
      <view class="hint">
        <view>{{ userInfoStore.nickName || "去登录" }} </view>
      </view>
    </view>

    <view class="content">
      <view class="grid-container">
        <view class="grid-item" v-for="(listItem, listIndex) in sumList" :key="listIndex">
          <view class="count">{{ listItem.count }}</view>
          <text class="grid-text">{{ listItem.title }}</text>
        </view>
      </view>
    </view>

    <view class="tag-box">
      <view class="tag"></view>
      我创建的活动
    </view>
    <scroll-view-list
      ref="tableCRef"
      :params="viewListParams.params"
      :init-params="viewListParams.createdParams"
      :api="cApi"
      height="auto"
      url="/pages/templateDetail/index"
    />

    <view class="tag-box">
      <view class="tag"></view>
      我参与的活动
    </view>
    <scroll-view-list
      ref="tableRRef"
      :api="rApi"
      :params="viewListParams.params"
      :init-params="viewListParams.relevanceParams"
      height="auto"
      url="/pages/templateDetail/index"
    />
  </view>
</template>

<script setup lang="ts">
import { activityFindActivityPage } from "$api/template.ts";
import scrollViewList from "@/components/ScrollViewList/index.vue";

const userInfoStore = useUserInfoStore();

const cApi = userInfoStore.openId ? activityFindActivityPage : null;
const rApi = userInfoStore.openId ? activityFindActivityPage : null;
const { proxy } = getCurrentInstance();
let viewListParams = {
  params: {
    _type: "apply",
    id: "$modelId",
    activityId: "$id"
  },
  createdParams: {
    wxCode: userInfoStore.openId
  },
  relevanceParams: {
    wxCode: userInfoStore.openId
  }
};

let sumList = ref([
  {
    count: "0",
    title: "浏览总量",
    prop: "pageView"
  },
  {
    count: "0",
    title: "活动总量",
    prop: "participation"
  },
  {
    count: "0",
    title: "分享总量",
    prop: "shareCount"
  }
]);

onLoad(() => {
  const params = uni.getStorageSync(CONSTANT_PARAMS.MY_HOME_PARAMS);

  if (params.toPath) {
    uni.removeStorageSync(CONSTANT_PARAMS.MY_HOME_PARAMS);
    uni.navigateTo({
      url: routerTransitionParams(params, params.toPath)
    });
  }
});

onShow(() => init());

const init = () => {
  // 用户已登录获取对应用户信息
  if (userInfoStore.openId) {
    // 获取浏览量
    // userTemplateFindSumCountByUserId({ userId: userInfoStore.openId }).then(data => {
    //   for (let item of sumList.value) {
    //     item.count = data[item.prop];
    //   }
    // });
  }

  nextTick(() => {
    proxy.$refs.tableCRef.getTableList();
    proxy.$refs.tableRRef.getTableList();
  });
};
</script>
<style lang="scss" scoped src="./index.scss"></style>
